package cdc.cs.service;

import cdc.cs.common.HttpWithAuthService;
import cdc.cs.config.ODataConfig;
import cdc.cs.entity.BusinessPartnersRoleVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: public class ODataPartnersRoleService {
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:28
 * @Version: 1.0
 */
@Service
@Slf4j
public class ODataPartnersRoleService {

    private List<BusinessPartnersRoleVO> roleVOList;

    /**
     * 获取单个角色
     * @param url
     * @return
     */
    public String getOneRole(String url) {
        String result = null;
        int count = 2;
        while (count > 0) {
            try {
                result = HttpWithAuthService.getService(url);
            } catch (IOException e) {
                log.error("获取Partner总数网络异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (StringUtils.isEmpty(result)) {
                count--;
            } else {
                break;
            }

        }
        return result;
    }

    /**
     * 解析角色
     * @param response
     * @return
     */
    private List<BusinessPartnersRoleVO> parseAddressJson(String response) {
        List<BusinessPartnersRoleVO> list = new ArrayList<>();
        JSONObject obj = (JSONObject) JSONObject.parse(response);
        JSONObject d = (JSONObject) obj.get("d");
        JSONArray results = d.getJSONArray("results");
        if (results.size() > 0) {
            for (int i = 0; i < results.size(); i++) {
                JSONObject object = results.getJSONObject(i);
                BusinessPartnersRoleVO roleVO = new BusinessPartnersRoleVO();
                roleVO.setBusinessPartner(object.getString("BusinessPartner"));
                roleVO.setBusinessPartnerRole(object.getString("BusinessPartnerRole"));
                roleVO.setValidFrom(object.getString("ValidFrom"));
                roleVO.setValidTo(object.getString("ValidTo"));
                list.add(roleVO);
            }
            this.roleVOList = list;
            return list;
        } else {
            return null;
        }

    }

    /**
     * 数据转换
     * @param roleVO
     * @return
     */
    public JSONObject dataChange(BusinessPartnersRoleVO roleVO) {
        JSONObject result = new JSONObject();
        result.put("BusinessPartner", roleVO.getBusinessPartner());
        result.put("BusinessPartnerRole", roleVO.getBusinessPartnerRole());
        result.put("ValidFrom", roleVO.getValidFrom());
        result.put("ValidTo", roleVO.getValidTo());
        return result;
    }

    public boolean savePartnerRole(BusinessPartnersRoleVO roleVO) {
        JSONObject jsonObject = dataChange(roleVO);
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.postService(ODataConfig.targetPartnerRole, jsonObject);
                flag = true;
            } catch (IOException e) {
                log.error("保存PartnerRole出现异常，未保存数据:{}，地址为:{}", jsonObject, ODataConfig.targetPartnerRole);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
        return flag;
    }

    public boolean savePartnerRole(String url) {
        List<BusinessPartnersRoleVO> list = queryOneAddress(url);
        if (list == null) {
            return false;
        } else {
            for (BusinessPartnersRoleVO role : list) {
                boolean resule = savePartnerRole(role);
                if (!resule) {
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * 删除角色
     * @param url
     */
    public void deletePartnerRole(String url) {
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.deleteService(url);
                flag = true;
            } catch (IOException e) {
                log.error("删除TargetPartnerRole异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
    }

    /**
     * 获取数据
     * @param url
     * @return
     */
    public List<BusinessPartnersRoleVO> queryOneAddress(String url) {
        String oneAddress = getOneRole(url);
        if (StringUtils.isEmpty(oneAddress)) {
            return null;
        } else {
            List<BusinessPartnersRoleVO> list = parseAddressJson(oneAddress);
            return list;
        }
    }
}
